import xml.sax.handler

# Modes
#  0: None
#  1: name

ROW = '<tr><td>%s</td></tr>'

class RecomendationsHandler(xml.sax.handler.ContentHandler):
    def __init__(self):
        self.artists = []
        self.mode = 0
        self.name = ''
        self.setup = False

    def startDocument(self):
        self.artists = []
        self.mode = 0
        self.name = ''
        self.setup = False
        
    def endDocument(self):
        self.setup = True

    def startElement(self, name, attributes):
        if name == 'name':
            self.mode = 1
 
    def characters(self, data):
        if self.mode == 1:
            self.name += data
     
    def endElement(self, name):
        self.mode = 0
        if name == 'artist':
            self.artists.append(ROW % self.name)
            self.name = ''
      
    def toString(self, howmany):
        string = '<table cellpadding="0" cellspacing="0"><tr><td><b>Artist</b></td></tr>'
        for i in range(howmany):
            string += self.artists[i]
        string += '</table>'
        return string
